package com.ly.demo.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.ly.demo.entity.SmResult;
import com.ly.demo.entity.UserEntity;
import com.ly.demo.service.UserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * @Author liuyang
 * @Date 2023/7/11 10:23
 **/
@RestController
@RequestMapping("/mp")
public class MPController {
    @Resource
    private UserService service;

    @GetMapping("/getUserById")
    public SmResult getUserById(@RequestParam Integer id) {
        return service.getUserById(id);
    }

    @GetMapping("/yesterDay")
    public SmResult yesterDay() {
        return service.yesterDay();
    }

    @GetMapping("/oderby")
    public SmResult oderby() {
        return service.oderby();
    }

    @PostMapping("/insert")
    @Transactional
    public SmResult insert() {
        service.save(new UserEntity().setName("大聪明"));
        try {
            Thread.sleep(500000000L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return SmResult.ok();
    }

    @PostMapping("/updateUser")
    public SmResult updateUser() {
        service.lambdaUpdate().set(UserEntity::getName,"大智慧").update();
        return SmResult.ok();
    }
    public static void main(String[] args) {

        HashSet hashSet = new HashSet();

        hashSet.add("1");
        hashSet.add("2");

        // 使用hashCode方法获取字符串的哈希值
        int hash = "liuab".hashCode();
        List<String> list = Arrays.asList("1", "2");
        System.out.println(list.get(Math.abs(hash) % 2));

        System.out.println(
                "==========================="
        );

        String startTimeStr = "2023-03-29 10:19:57";
        String endTimeStr = "2028-03-29 10:19:57";

        // 将字符串转换为日期类型
        DateTime startTime = DateUtil.parse(startTimeStr);
        DateTime endTime = DateUtil.parse(endTimeStr);

        // 计算年份差异
        long years = DateUtil.betweenYear(startTime, endTime, false);

        System.out.println("年份差异：" + years + " 年");


        // 增加 365 * 5 天
        DateTime newTime = DateUtil.offset(startTime, DateField.DAY_OF_MONTH, 2);

        // 将新的时间转换为字符串
        String newTimeStr = DateUtil.format(newTime, "yyyy-MM-dd HH:mm:ss");

        System.out.println("增加 365 * 5 天后的时间：" + newTimeStr);
    }
}
